$(function () {

    $(".remove_Button").click(function (e) {
        var url = window.location.href;
        var idarr = url.split("?id=")
        var new_Product = document.getElementsByClassName("new_Product")
        var commodityId = document.getElementsByClassName("commodityId")
        var arr = new Array(new_Product.length+1);
        for (let i = 0; i <new_Product.length ; i++) {
            if (new_Product[i].getAttribute("boolean") == "true"){
                arr[i] = commodityId[i].innerText
            }
        }
        arr[new_Product.length+1] = idarr[1]
        $.ajax({
            //请求
            type:"GET",
            //地址
            url:"/delete/cart",
            //传值
            data: "arr="+arr,
            success:function () {
                location.reload();
            },
            beforeSend:function () {

            }
        })
    });

    function total() {
        var true_index = 0 ;
        var false_index = 0;
        var number = $(".number");
        for (let i = 0; i < new_Product.length; i++) {
            if ($(new_Product[i]).attr("boolean") == "true"){
                true_index += parseInt($(number[i]).val())
                $(".total_Number").text(true_index)
            }else{
                false_index++;
            }
            if (false_index == new_Product.length){
                $(".total_Number").text("0")
            }
        }
    }

    function total_Number() {
        var subtotal_Quantity = $(".subtotal_Quantity")
        var total_Price = $(".total_Price")
        var boolean = $(".new_Product");
        var sun = 0 ;
        for (let i = 0; i < $(".new_Product").length; i++) {
            if ($(boolean[i]).attr("boolean") == "true"){
                sun += parseFloat($(subtotal_Quantity[i]).text());
            }else if ($(boolean[i]).attr("boolean") == "false"){
                console.log(1)
            }
        }
        total_Price.text(sun.toFixed(2))
    }
    total_Number();

    var new_Product = $(".new_Product")
    new_Product.click(function () {
        total_Number();
    })

    var img = $(".options_Img");
    var user = $(".username");
    var more = $(".more");
    $(document).ready(function () {
        user.mouseover(function () {
            img.attr('src','/Web_img/Shopping_cart_1.png');
            more.stop().slideDown("slow");
        });
        user.mouseout(function () {
            img.attr('src','/Web_img/Shopping_cart_2.png');
            more.stop().slideUp("slow");
        });
    });
    $(document).ready(function () {
        more.mouseover(function () {
            more.css("display","block");
            more.stop().slideDown("slow");
        });
        more.mouseout(function () {
            more.css("display","none");
            more.stop().slideUp("slow");
        });
    });

    var new_Reduce = $(".new_Reduce");
    var New_Add = $(".New_Add");
    var hint = $(".hint");
    var remove_Img = $(".remove_Img");
    var button = $(".button");
    var hint_Word = $(".hint_Word");
    var quantity_Hint = $(".quantity_Hint");
    var subtotal_Quantity = $(".subtotal_Quantity");
    //仓库数量
    var storehouse = 5;

    //增加
    $(document).ready(function(){
        New_Add.click(function () {
            var number = $(".number").val(1);
            var oop = new JQuery_oop();
            //下标
            var Index = parseInt($(this).attr("add"))-1;
            if (oop.is_number($(number[Index]).val())){
                //还可以购买数量
                if ($(number[Index]).attr("value") == ""){
                    hint_Word.text("输入的数量只能是数字！")
                    hint.css("display","block")
                    $(number[Index]).val(0)
                }
                var remaining = storehouse-parseInt($(number[Index]).attr("value"));
                if (remaining <= storehouse){
                    $(quantity_Hint[Index]).text("你还可以购买"+(remaining-1)+"件")
                }

                if ($(number[Index]).attr("value") >= 999){
                    hint_Word.text("修改数量不能大于999+")
                    hint.css("display","block")
                    //仓库数量还可以购买的数量
                }if (remaining == 1 ){
                    $(quantity_Hint[Index]).text("")
                    hint_Word.text("商品加入购物车数量超过限购数")
                    hint.css("display","block")
                } else {
                    $(number[Index]).attr("value",parseInt($(number[Index]).attr('value')) + 1);
                    $(number[Index]).val($(number[Index]).attr("value"))
                    var subtotal = parseFloat($(subtotal_Quantity[Index]).text()) * 2;
                    $(subtotal_Quantity[Index]).text(subtotal)
                    var forward_Remaining = (remaining-1);
                    if (forward_Remaining <= 5){
                        if (forward_Remaining != 0){
                            $(quantity_Hint[Index]).text("你还可以购买"+(forward_Remaining-1)+"件")
                        }
                    }else{
                        $(quantity_Hint[Index]).text("")
                    }
                }
            }else {
                var number = document.getElementsByClassName("number")
                if (number.value.test(/[0-9]/)){
                    hint_Word.text("输入的数量只能是数字！")
                    hint.css("display","block")
                    $(number[Index]).val(1)
                }
            }
            total();
            total_Number();
        })
    })
    remove_Img.click(function () {
        hint.css("display","none")
    })
    button.click(function () {
        hint.css("display","none")
    })
    //减少
    new_Reduce.click(function () {
        var number = $(".number").val(1);

        var oop = new JQuery_oop();
        //下标
        var Index = parseInt($(this).attr("add"))-1;

        if (oop.is_number($(number[Index]).val())){
            var remaining = storehouse-parseInt($(number[Index]).attr("value"));
            if ($(number[Index]).attr("value") <= 1){
                hint_Word.text("修改数量不能小于0")
                hint.css("display","block")
            }else if ($(number[Index]).attr("value") > 999){
                hint_Word.text("修改数量不能大于999+")
                hint.css("display","block")
            }else {
                $(number[Index]).attr("value",parseInt($(number[Index]).attr('value')) - 1);
                $(number[Index]).val($(number[Index]).attr("value"))
                var forward_Remaining = (remaining+1);
                var subtotal = parseFloat($(subtotal_Quantity[Index]).text())/2;
                $(subtotal_Quantity[Index]).text(subtotal)
                if (forward_Remaining <= 5){
                    if (forward_Remaining != 0){
                        $(quantity_Hint[Index]).text("你还可以购买"+forward_Remaining+"件")
                    }
                }else{
                    $(quantity_Hint[Index]).text("")
                }
            }
        }else {
            var number = document.getElementsByClassName("number")
            if (number.value.test(/[0-9]/)){
                hint_Word.text("输入的数量只能是数字！")
                hint.css("display","block")
                $(number[Index]).val(1)
            }
        }
        total();
        total_Number();
    });



    var delete_Img = $(".delete_Img")
    $(document).ready(function () {
        delete_Img.mouseover(function () {
            var Index = parseInt($(this).attr("add"))-1;
            $(delete_Img[Index]).attr('src','/Web_img/×_red.png');
        });
        delete_Img.mouseout(function () {
            var Index = parseInt($(this).attr("add"))-1;
            $(delete_Img[Index]).attr('src','/Web_img/×.png');
        });
    });
    var remove_Index = 0 ;
    var remove = $(".hint_Remove")
    delete_Img.click(function () {
        var Index = parseInt($(this).attr("add"))-1;
        remove_Index = Index;
        remove.css("display","block")
    });
    var Cancel = $(".cancel");
    Cancel.click(function () {
        remove.css("display","none")
    });
    var remove_Img = $(".remove_Img");
    remove_Img.click(function () {
        remove.css("display","none")
    });
    var remove_Button = $(".remove_Button");
    var new_Commodity = $(".new_Commodity")
    remove_Button.click(function () {
        $(new_Commodity[remove_Index]).remove()
        remove.css("display","none")
    });
    //全选
    var choose_Product = $(".choose_Product")
    var new_Product = $(".new_Product")
    choose_Product.click(function () {
        var judge = choose_Product.attr("checked_judge")
        if (judge == "false"){
            new_Product.prop("checked",true)
            choose_Product.attr("checked_judge","true");
            new_Product.attr("boolean","true")
        }else if (judge == "true"){
            new_Product.prop("checked",false)
            choose_Product.attr("checked_judge","false");
            new_Product.attr("boolean","false")

        }
        total();
        total_Number();
    });
    //单选
    new_Product.click(function () {
        var Index = parseInt($(this).attr("add"))-1;
        var state = $(new_Product[Index]).attr("boolean");
        if (state=="true"){
            $(new_Product[Index]).attr("boolean","false")
            //主复选框
            choose_Product.prop("checked",false)
            choose_Product.attr("checked_judge","false");
        }else if (state=="false"){
            $(new_Product[Index]).attr("boolean","true")
            var sun = 0 ;
            for (let i = 0; i < new_Product.length; i++) {
               if ($(new_Product[i]).attr("boolean") == "true"){
                   sun+=1;
                   if (sun == 3){
                       //主复选框
                       choose_Product.prop("checked",true)
                       choose_Product.attr("checked_judge","true");
                   }
               }
            }

        }
        total();
        total_Number();
    })
    total();


    var commodity_shopping = $(".commodity_shopping1")
    var commodity = $(".commodity")
    $(document).ready(function () {
        commodity_shopping.mouseover(function () {
            commodity.mouseover(function () {
                var i = this.children
                $(i[4]).css({"display":"block"});
                $(i[4]).get(0).style.transform="translate3d(-10px,-20px,500px)"
            });
            commodity.mouseout(function () {
                var i = this.children
                $(i[4]).css({"display":"none"});
            });
        });
    });
    $(".add_Shopping").click(function (e) {
        var url = window.location.href;
        var split = url.split("?id=")
        var i = e.target
        console.log(i )
        var index = i.getAttribute("add");
        var product_id = document.getElementsByClassName("id");
        var product = i.parentNode.parentNode.firstChild;

        var product_price = product.nextElementSibling.nextElementSibling.nextElementSibling
        console.log(product_id[index-1])
        var arr = [split[1],product_id[index-1].innerText,product_price.innerText]
        var bottom = document.getElementsByClassName("other_Bottom")
        $.ajax({
            //请求
            type:"GET",
            //地址
            url:"/addcart",
            //传值
            data: "arr="+arr,
            success:function (res) {
                location.reload();
                bottom[0].style.marginTop = parseInt(bottom[0].style.marginTop.replace('px',''))+150+"px"
            },
            beforeSend:function () {

            }
        })
    })
    $(".settlement_Price").click(function (e) {
        var total_Price = document.getElementsByClassName("total_Price")
        var initial = 0.00+""
        var text = total_Price[0].innerText;
        if (!parseInt(text) == initial){
            var url = window.location.href;
            var idarr = url.split("?id=")
            var new_Product = document.getElementsByClassName("new_Product")
            var commodityId = document.getElementsByClassName("commodityId")
            var total_Price = document.getElementsByClassName("total_Price")
            var number = document.getElementsByClassName("number")
            var Product_Number = "";
            var Number_Products = "";
            for (let i = 0; i <new_Product.length ; i++) {
                if (new_Product[i].getAttribute("boolean") == "true"){
                    Product_Number += ","+commodityId[i].innerText
                    Number_Products += ","+number[i].value
                }
            }
            window.location = "/settlement?id="+idarr[1]+"&cid="+Product_Number+"&number="+Number_Products+"&totalcost="+total_Price[0].innerText
        }
    });
})